package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.qo.CustomerQueryObject;
import cn.wolfcode.rbac.qo.EmployeeQueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.IStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IStatusService statusService;

//    @RequestMapping("/list")
//    public ModelAndView list(EmployeeQueryObject qo) {
//        ModelAndView mv = new ModelAndView();
//        List<Employee> employees = employeeService.selectList(qo);
//        mv.addObject("employees", employees);
//        List<Department> departments = departmentService.selectList();
//        mv.addObject("departments",departments);
//        mv.addObject("qo", qo);
//        mv.setViewName("/employee/list");
//        return mv;
//    }


    @RequestMapping("/potentialList")
    public ModelAndView list(CustomerQueryObject qo){
        if(qo.getKeyword()==null||qo.getKeyword().equals("")) {
            System.out.println("qo==null");
            ModelAndView mv = new ModelAndView();
            mv.addObject("customers", customerService.selectAll());
            mv.addObject("statuses",statusService.selectAll());
            mv.setViewName("/customer/list");
            return mv;
        }else {
            System.out.println("qo!=null");
            ModelAndView mv = new ModelAndView();
            mv.addObject("customers", customerService.selectList(qo));
            mv.addObject("statuses",statusService.selectAll());
            mv.setViewName("/customer/list");
            return mv;
        }
    }

    @RequestMapping("/delete")
    public ModelAndView delete(Long id){
        customerService.deleteById(id);
        ModelAndView mv=list(new CustomerQueryObject());
        return mv;

    }

    @RequestMapping("/input")
    public ModelAndView input(Long id){
        //添加
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/customer/input");
        if(id==null){

        }else {
            mv.addObject("customer",customerService.selectById(id));
        }

        return mv;
    }

    @RequestMapping("/saveOrUpdate")
    public ModelAndView saveOrUpdate(Customer customer){
        //添加
        if(customer.getId()==null){
            customerService.insertOne(customer);
        }else {//更新
            customerService.updateById(customer);
        }
        ModelAndView mv=list(new CustomerQueryObject());
        return mv;
    }
}
